﻿#include "FontEditEntry.h"

#include <QFontDatabase>
#include <QLabel>
#include <QLabel>

#include "StringSelectEditEntry.h"
#include "IntegerEditEntry.h"
#include "BooleanEditEntry.h"
#include "EnumEditEntry.h"

struct FontEditEntry::FontEditEntryPrivate {
    QPointer<QLabel> labelFontThumbnai;
    QPointer<QLabel> labelInfo;
    QPointer<StringSelectEditEntry> entryFamilies;
    QPointer<IntegerEditEntry> entryPointSize;
    QPointer<BooleanEditEntry> entryBold;
    QPointer<BooleanEditEntry> entryItalic;
    QPointer<BooleanEditEntry> entryUnderline;
    QPointer<BooleanEditEntry> entryOverline;
    QPointer<BooleanEditEntry> entryFixedPitch;
    QPointer<EnumEditEntry> entryWeight;
    QPointer<BooleanEditEntry> entryAntialias;
    QPointer<EnumEditEntry> entryHintingPreference;
};

FontEditEntry::~FontEditEntry() { }

FontEditEntry::FontEditEntry(QWidget *parent) : EditEntry{parent} {
    p.reset(new FontEditEntryPrivate);
    initUi();
    retranslateUi();
}

void FontEditEntry::setValue(const QFont &value) {
    const auto &infoText = QString("[%1,%2]").arg(value.family()).arg(value.pointSize());
    p->labelInfo->setText(infoText);
    p->labelFontThumbnai->setFont(value);

    p->entryFamilies->setValue(value.family());
    p->entryPointSize->setValue(value.pointSize());
    p->entryBold->setValue(value.bold());
    p->entryItalic->setValue(value.italic());
    p->entryUnderline->setValue(value.underline());
    p->entryOverline->setValue(value.overline());
    p->entryFixedPitch->setValue(value.fixedPitch());
    p->entryWeight->setValue(value.weight());
    p->entryAntialias->setValue(value.styleStrategy() & QFont::PreferAntialias);
    p->entryHintingPreference->setValue(value.hintingPreference());
}

QFont FontEditEntry::value() {
    QFont font;
    return font;
}

void FontEditEntry::initUi() {
    p->labelFontThumbnai = new QLabel("A");
    p->labelFontThumbnai->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    addEditControll(p->labelFontThumbnai, 0);

    p->labelInfo = new QLabel;
    addEditControll(p->labelInfo);

    QFontDatabase database;
    p->entryFamilies = new StringSelectEditEntry;
    p->entryFamilies->setStringList(database.families());
    appendChildEntry(p->entryFamilies);

    p->entryPointSize = new IntegerEditEntry;
    appendChildEntry(p->entryPointSize);

    p->entryBold = new BooleanEditEntry;
    appendChildEntry(p->entryBold);

    p->entryItalic = new BooleanEditEntry;
    appendChildEntry(p->entryItalic);

    p->entryUnderline = new BooleanEditEntry;
    appendChildEntry(p->entryUnderline);

    p->entryOverline = new BooleanEditEntry;
    appendChildEntry(p->entryOverline);

    p->entryFixedPitch = new BooleanEditEntry;
    appendChildEntry(p->entryFixedPitch);

    p->entryWeight = new EnumEditEntry;
    p->entryWeight->setMetaEnum(QMetaEnum::fromType<QFont::Weight>());
    appendChildEntry(p->entryWeight);

    p->entryAntialias = new BooleanEditEntry;
    appendChildEntry(p->entryAntialias);

    p->entryHintingPreference = new EnumEditEntry;
    p->entryHintingPreference->setMetaEnum(QMetaEnum::fromType<QFont::HintingPreference>());
    appendChildEntry(p->entryHintingPreference);
}

void FontEditEntry::retranslateUi() {
    p->entryFamilies->setTitle(tr("family"));
    p->entryPointSize->setTitle(tr("pointize"));
    p->entryBold->setTitle((tr("bold")));
    p->entryItalic->setTitle(tr("itatic"));
    p->entryUnderline->setTitle(tr("underline"));
    p->entryOverline->setTitle(tr("overline"));
    p->entryFixedPitch->setTitle(tr("fixedPitch"));
    p->entryWeight->setTitle(tr("weight"));
    p->entryAntialias->setTitle(tr("antialias"));
    p->entryHintingPreference->setTitle(tr("hintingPreference"));
}


